package com.zch.mobile;

import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.base.dialect.PaginationSupport;
import com.base.mobile.MobException;
import com.base.mobile.MobileInfo;
import com.base.mobile.server.annotation.Mobile;
import com.base.mobile.server.annotation.MobileMethod;
import com.base.util.DateUtil;
import com.bf.common.proto.MCommon.MRet;
import com.bf.common.proto.MZxMessage.MPrincipal;
import com.bf.common.proto.MZxMessage.MPrincipalList;
import com.zch.dao.model.Principal;
import com.zch.dao.model.PrincipalArea;
import com.zch.dao.model.PrincipalAreaExample;
import com.zch.dao.model.PrincipalExample;
import com.zch.daoEx.model.PrincipalEx;
import com.zch.service.PrincipalAreaService;
import com.zch.service.PrincipalService;
import com.zch.service.RzService;
import com.zch.dao.model.Rz;

@Component
@Mobile
public class MAppPrincipal {
    
    @Autowired
    private PrincipalService principalService;
    @Autowired
    private RzService rzService;
    @Autowired
    private PrincipalAreaService principalAreaService;
    
    /**
    @api MPrincipalList MPrincipalList  //资本列表(分页)
    @return MPrincipalList
    */
    @MobileMethod(methodno = "MPrincipalList", isPage = true)
    public MPrincipalList.Builder principalList(Integer page, Integer limit) throws Exception {
        // 捞数据
        PrincipalExample example = new PrincipalExample();
        example.createCriteria().andIsVerifyEqualTo(1);
        example.setOrderByClause("create_time desc");
        PaginationSupport.byPage(page, limit, false);
        List<Principal> list = principalService.selectByExample(example);
        
        // 返回结果
        MPrincipalList.Builder ret = MPrincipalList.newBuilder();
        for (Principal principal : list){
            MPrincipal.Builder mini = MPrincipal.newBuilder();
            mini.setId(principal.getId());
            mini.setInfo(principal.getInfo());
            mini.setOrganizationInfo(principal.getOrganizationInfo());
            mini.setTime(DateUtil.dateToStr(principal.getCreateTime(), DateUtil.YYMMMM));
            mini.setImgs(principal.getImgs());
            ret.addMini(mini);
        }
        return ret;
    }

    /**
    @api MPrincipal MPrincipal  //资本详情
    @param required string id   //资本id
    @return MPrincipal
    */
    @MobileMethod(methodno = "MPrincipal")
    public MPrincipal.Builder principal(String id) throws Exception {
        // 检查参数
        if (StringUtils.isBlank(id))
        {
            throw new MobException("id");
        } 
        
        // 捞数据
        PrincipalEx principal = principalService.selectAppDetailLists(id);
        if (principal == null ){
            throw new MobException(MZchError.CONTENT_NOT_EXIST);
        }
        
        // 返回结果
        MPrincipal.Builder ret = MPrincipal.newBuilder();
        ret.setArea(principal.getArea());
        ret.setCorporateUser(principal.getCorporateUser());
        ret.setId(principal.getId());
        ret.setImgs(principal.getImgs());
        ret.setInfo(principal.getInfo());
        ret.setTime(DateUtil.dateToStr(principal.getCreateTime(), DateUtil.YYMMMM));
        ret.setRegistMoney(principal.getRegistMoney());
        ret.setRegistAddress(principal.getRegistAddress());
        ret.setOrganizationName(principal.getOrganizationName());
        ret.setOrganizationInfo(principal.getOrganizationInfo());
        ret.setManagerUser(principal.getManagerUser());
        ret.setManagerJob(principal.getManagerJob());
        ret.setManagerContact(principal.getManagerContact());
        ret.setTitle(principal.getTitle());
        ret.setUserId(principal.getNickName());
        
        return ret;
    }

    /**
    @api MAddPrincipal MAddPrincipal    //发布资本(登陆)
    @param optional string organizationName     //机构名称
    @param optional string area  //地区：省-市
    @param optional string registAddress    //注册地址
    @param optional string registMoney  //注册资金
    @param optional string corporateUser    //法人代表
    @param optional string managerUser  //具体负责人
    @param optional string managerJob //负责人职务
    @param optional string managerContact   //联系方式
    @param optional string organizationInfo     //机构概况----------------------------------------机构概括
    @param optional string info     //概况-------------------------------------投资方式
    @param optional string imgs     //图片
    @return MRet
    */
    @MobileMethod(methodno = "MAddPrincipal", isLogin = true)
    public MRet.Builder addPrincipal(MobileInfo mobileInfo, 
                                     String organizationInfo,
                                     String info) throws Exception{
        if (StringUtils.isBlank(organizationInfo)) throw new MobException("organizationInfo");
        if (StringUtils.isBlank(info)) throw new MobException("info");
        /*if (StringUtils.isBlank(tzly)) throw new MobException("tzly");*/
        MRet.Builder ret = MRet.newBuilder();
        if(info.length()>=1000){
            ret.setCode(-1);
            ret.setMsg("投资方式请控制在1000个字以内...");
            return ret;
        }
        if(organizationInfo.length()>=1000){
            ret.setCode(-1);
            ret.setMsg("机构概况请控制在1000个字以内...");
            return ret;
        }
        // 创建dao模型
        Principal principal = new Principal();
        principal.setOrganizationInfo(organizationInfo);
        principal.setUserId(mobileInfo.getUserid());
        principal.setInfo(info);
        principal.setIsVerify(0);
        /*principal.setTzly(tzly);*/
        // 插入数据
        int c = principalService.insert(principal);
        if (c == 0){
            throw new MobException(MZchError.SERVER_BUSY_ERROR);
        }
        
        // 返回结果
        
        ret.setCode(1);
        return ret;
    }
    
    /**
     * 投资领域
     * @return
     * @throws Exception
     */
    @MobileMethod(methodno = "MFindTzly", isLogin = true)
    public MPrincipalList.Builder MFindTzly()throws Exception{
        MPrincipalList.Builder ret = MPrincipalList.newBuilder();
        PrincipalAreaExample example = new PrincipalAreaExample();
        example.createCriteria();
        example.setOrderByClause("create_time desc");
        List<PrincipalArea> list = principalAreaService.selectByExample(example);
        
        for(PrincipalArea ex : list){
            MPrincipal.Builder c = MPrincipal.newBuilder();
            c.setId(ex.getId());
            c.setTitle(ex.getName());
            ret.addMini(c);
        }
        
        return ret;
    }
    
    /**
     * 我要融资
     * @param mobileInfo
     * @param name
     * @param phone
     * @param company
     * @param address
     * @param money
     * @param content
     * @return
     * @throws Exception
     */
    @MobileMethod(methodno = "MRz", isLogin = false)
    public JSONArray MRz()throws Exception{
        JSONArray array = new JSONArray();
        JSONObject object = new JSONObject();
        object.put("name", "陆沄洲");
        object.put("phone", "13861295821");
        object.put("email", "1955199110@qq.com");
        object.put("phone1", "0519-86628178");
        object.put("phone2", "0519-86628177");
        array.add(object);
        return array;
    }
}
